package com.ymttest.database.mongo.jproduct;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;

import com.ymt.database.MongoDBDriver;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.utils.YMTDateUtil;

public class LivesWapper {
	private MongoDBDriver mdd = YmtMongoDriverFactroy.getInstance_YmtProductDBdriverNewJava("Lives");
	
	public Map<String,Object> getByActivityId(int lid) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"lid\":" + lid + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	
	public int getCountByActivityId(int lid) throws Exception{
		return mdd.selectPartLimitToMap("{\"lid\":" + lid + "}",50).size();
	}
	
	public boolean update(Map<String, Object> searchMap,Map<String, Object> updateMap) throws Exception{
		return  mdd.update(searchMap, updateMap).isUpdateOfExisting();
	}
	/**
	 * 更新cover 
	 * @param lid
	 * @param cover
	 * @throws Exception
	 */
	public void updateCover(String lid,String cover) throws Exception{
		mdd.update("{\"lid\":" + lid + "}", "{\"cover\":\"" + cover + "\"}");
	}
	public ArrayList<Map> getLivesBySellId(int sellId) throws Exception { 
		String nowDate=YMTDateUtil.getDateForMongoFormat(0,-8,0);
		String whereStr="{'sid':"+sellId+",'action':1,'confirm':true,'start':{$lte:{$date:'"+nowDate+"'}},'end':{$gte:{$date:'"+nowDate+"'}}}";
		ArrayList<Map> list = mdd.selectPartLimitToMap(whereStr,999999);
		return list;
	} 
	   /**
	    * 根据活动Id获取直播信息
	    * @param sid
	    * @return
	    * @throws Exception
	    */
		public Map<String,Object> getLivesBylid(int lid) throws Exception {
			ArrayList<Map> list = mdd.selectPartLimitToMap("{\"lid\":" + lid + ",\"action\":1}",1);
		    
		    return list.size()>0?list.get(0):null;
		}
	/**
	 * 根据卖家Id列表获取卖家正在直播
	 * @param sellId
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getLivesListBySellerIds(int[] sellIds) throws Exception {
		String nowDate=YMTDateUtil.getDateForMongoFormat(0,-8,0);
		String str="";
		for(int i=0;i<sellIds.length;i++)
		{
			str+=sellIds[i]+",";
		}
		String whereStr="{'sid':{$in:[" + str.substring(0,str.length()-1) + "]},'action':1,'confirm':true,'start':{$lte:{$date:'"+nowDate+"'}},'end':{$gte:{$date:'"+nowDate+"'}}}";
		ArrayList<Map> list=mdd.selectPartLimitToMap(whereStr,9999999);
	    return list;
	}
	/**
	 * 根据卖家Id获取卖家正在直播
	 * @param sellId
	 * @return
	 * @throws Exception 
	 */
	public ArrayList<Map> getLivesBySellerId(int sellId) throws Exception {
		String nowDate=YMTDateUtil.getDateForMongoFormat(0,-8,0);
		String whereStr="{'sid':"+sellId+",'action':1,'confirm':true,'start':{$lte:{$date:'"+nowDate+"'}},'end':{$gte:{$date:'"+nowDate+"'}}}";
		ArrayList<Map> list=mdd.selectPartLimitToMap(whereStr,1);
	    return list;
	}
	
	public ArrayList<Map> getHistoryLivesBySellerId(int sellId,int livesNum) throws Exception {
		String nowDate=YMTDateUtil.getDateForMongoFormat(0,-8,0);
		String whereStr="{'sid':"+sellId+",'action':1,'confirm':true,'end':{$lte:{$date:'"+nowDate+"'}}}";
		String keyStr="{\"title\":1,\"vcover\":1,\"vurl\":1,\"action\":1,\"lid\":1,\"name\":1,\"pic\":1,\"add\":1,\"AddTime\":1,\"country\":1,\"end\":1,"
				+ "\"EndTime\":1,\"sid\":1,\"addr\":1,\"start\":1,\"content\":1,\"confirm\":1}";
		ArrayList<Map> list=mdd.selectPartLimitToMap(whereStr,keyStr,livesNum,"{'lid':-1}");
	    return list;
	}
	public static void main(String[] args) throws Exception {
//		System.out.println(new LivesWapper().getCountByActivityId(157289));
//
//		int c=1;
//		int a=c;
		ArrayList<Map> list=new LivesWapper().getLivesBySellerId(3383);
		
	}
}
